home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / INHERIT5.CPP < prev    next >
Text File  |  1991-04-28  |  3KB  |  125 lines

  1.                                   // Chapter 8 - Program 5
  2. #include "iostream.h"
  3.  
  4. class vehicle {
  5.    int wheels;
  6.    float weight;
  7. public:
  8.    vehicle(void) {wheels = 7; weight = 11111.0;}
  9.    void initialize(int in_wheels, float in_weight);
  10.    int get_wheels(void) {return wheels;}
  11.    float get_weight(void) {return weight;}
  12.    float wheel_loading(void) {return weight/wheels;}
  13. };
  14.  
  15.  
  16.  
  17.  
  18. class car : public vehicle {
  19.    int passenger_load;
  20. public:
  21.    car(void) {passenger_load = 4;}
  22.    void initialize(int in_wheels, float in_weight, int people = 4);
  23.    int passengers(void) {return passenger_load;}
  24. };
  25.  
  26.  
  27.  
  28.  
  29. class truck : public vehicle {
  30.    int passenger_load;
  31.    float payload;
  32. public:
  33.    truck(void) {passenger_load = 3; payload = 22222.0;}
  34.    void init_truck(int how_many = 2, float max_load = 24000.0);
  35.    float efficiency(void);
  36.    int passengers(void) {return passenger_load;}
  37. };
  38.  
  39.  
  40.  
  41.  
  42. main()
  43. {
  44. vehicle unicycle;
  45.  
  46. // unicycle.initialize(1, 12.5);
  47.    cout << "The unicycle has " << 
  48.                                 unicycle.get_wheels() << " wheel.\n";
  49.    cout << "The unicycle's wheel loading is " << 
  50.          unicycle.wheel_loading() << " pounds on the single tire.\n";
  51.    cout << "The unicycle weighs " << 
  52.                              unicycle.get_weight() << " pounds.\n\n";
  53.  
  54. car sedan;
  55.  
  56. // sedan.initialize(4, 3500.0, 5);
  57.    cout << "The sedan carries " << sedan.passengers() << 
  58.                                                     " passengers.\n";
  59.    cout << "The sedan weighs " << sedan.get_weight() << " pounds.\n";
  60.    cout << "The sedan's wheel loading is " << 
  61.                     sedan.wheel_loading() << " pounds per tire.\n\n";
  62.  
  63. truck semi;
  64.  
  65. // semi.initialize(18, 12500.0);
  66. // semi.init_truck(1, 33675.0);
  67.    cout << "The semi weighs " << semi.get_weight() << " pounds.\n";
  68.    cout << "The semi's efficiency is " << 
  69.                           100.0 * semi.efficiency() << " percent.\n";
  70. }
  71.  
  72.  
  73.  
  74.  
  75.               // initialize to any data desired
  76. void
  77. vehicle::initialize(int in_wheels, float in_weight)
  78. {
  79.    wheels = in_wheels;
  80.    weight = in_weight;
  81. }
  82.  
  83.  
  84.  
  85. void
  86. car::initialize(int in_wheels, float in_weight, int people)
  87. {
  88.    passenger_load = people;
  89.    vehicle::initialize(in_wheels, in_weight);
  90. }
  91.  
  92.  
  93.  
  94.  
  95. void
  96. truck::init_truck(int how_many, float max_load)
  97. {
  98.    passenger_load = how_many;
  99.    payload = max_load;
  100. }
  101.  
  102.  
  103.  
  104. float
  105. truck::efficiency(void)
  106. {
  107.    return payload / (payload + get_weight());
  108. }
  109.  
  110.  
  111.  
  112.  
  113. // Result of execution
  114. //
  115. // The unicycle has 7 wheel.
  116. // The unicycle's wheel loading is 1587.285767 pounds on the single tire.
  117. // The unicycle weighs 11111 pounds.
  118. //
  119. // The sedan carries 4 passengers.
  120. // The sedan weighs 11111 pounds.
  121. // The sedan's wheel loading is 1587.285767 pounds per tire.
  122. //
  123. // The semi weighs 11111 pounds.
  124. // The semi's efficiency is 66.666667 percent.
  125.